<?php
// +----------------------------------------------------------------------
// | HkCms Components
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace libs\system;

class Components {

    static private $_components = array(
        'Url' => array(
            'class' => '\\libs\\system\\Url',
        ),
        'Cloud' => array(
            'class' => '\\libs\\system\\Cloud',
        ),
        'CloudDownload' => array(
            'class' => '\\libs\\system\\CloudDownload',
        ),
        'Html' => array(
            'class' => '\\libs\\system\\Html',
        ),
        'UploadFile' => array(
            'class' => '\\libs\\util\\UploadFile',
        ),
        'Dir' => array(
            'class' => '\\libs\\util\\Dir',
        ),
       'Content' => [
           'class' => '\\libs\\system\\Content',
        ],
        'ContentOutput' => array(
            'class' => '\\content_output',
        ),

       'Tree' => [
            'class' => '\\libs\\util\\Tree',
        ],
    );

    public function __construct($_components = array()) {
        if (!empty($_components)) {
            $this->setComponents($_components);
        } else {
            $components = config('components');
            $this->setComponents(is_array($components)?$components:[]);
        }
    }

    public function __get($name) {
        if (isset(self::$_components[$name])) {
            $components = self::$_components[$name];
            if (!empty($components['class'])) {
                $class = $components['class'];
                unset($components['class'], $components['path']);
                if (class_exists($class)) {
                    $this->$name = new $class;
                    return $this->$name;
                }
            }
        }
    }

    /**
     * 连接
     * @access public
     * @param array $_components  配置数组
     * @return object
     */
    static public function getInstance($_components = array()) {
        static $systemHandier;
        if (empty($systemHandier)) {
            $systemHandier = new Components($_components);
        }
        return $systemHandier;
    }

    /**
     * 设置$_components
     * @param array $_components
     */
    public function setComponents($_components = array()) {
        self::$_components = array_merge(self::$_components, $_components);
    }

}
